﻿using System;
using System.Threading;
using WithRefsTest.Client.MySvc;
using WithRefsTest.DataModels;

namespace WithRefsTest.Client
{
	public static class ProxyHelper
	{
		private static MyServiceClient _proxy;
		private static readonly object _lockObj = new object();

		private static MyServiceClient Proxy
		{
			get
			{
				if (ViewModelBase.IsDesignMode) return null;
				if (_proxy == null)
				{
					lock (_lockObj)
					{
						Thread.MemoryBarrier();
						if (_proxy == null)
						{
							_proxy = new MyServiceClient();
						}
					}
				}
				return _proxy;
			}
		}

		public static void GetEntity1(int id, Action<Entity1> callback)
		{
			Proxy.BeginGetEntity1(id, GetEntity1Callback, callback);
		}

		private static void GetEntity1Callback(IAsyncResult ar)
		{
			var res = Proxy.EndGetEntity1(ar);
			var callback = (Action<Entity1>) ar.AsyncState;
			callback(res);
		}
	}
}